package com.Policing.Check;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.Policing.Ip_set.Ip_Set;
import com.Policing.R;
import com.Policing.CanniBalize.Shift_site;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/5/18.
 */

public class Check_alter extends Activity {
    private ImageView imageview;
    private TextView tv_title;
    private Handler handler = new Handler();
    private Button upload;
    private String url = "http://"+ Ip_Set.IP+"/api/assetUpdate";
    private LinearLayout preserver_lt,place_lt;
    private String preserver_id="",place_id="",de_id="";
    private TextView AssetNumber,AssetName,DeptId,Type;//编号，名称，部门，类别
    private TextView preserver,place;//保管人，存放地方
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_alter);
        inview();
        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!de_id.equals("")&&!place_id.equals("")){
                    loading();
                }
            }
        });
        preserver_lt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Check_alter.this, Query_Wr.class);
                startActivityForResult(intent,2);
            }
        });
        place_lt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Check_alter.this, Shift_site.class);
                startActivityForResult(intent,1);
            }
        });
    }

    private void loading() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象。
                FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体
                formBody.add("assetNumber",AssetNumber.getText().toString())
                        .add("worker",preserver_id)
                        .add("dept",de_id)
                        .add("place",place_id); //传递键值对参数
                Request request = new Request.Builder()//创建Request 对象。
                        .url(url)
                        .post(formBody.build())//传递请求体
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        //回调方法的使用与get异步请求相同
                        Log.i("123123", "error");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String responseData = response.body().string();
                        Log.i("123123",responseData);
                        //回调方法的使用与get异步请求相同
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    JSONObject json = new JSONObject(responseData);
                                    if (json.getString("state").equals("1")){
                                        Intent intent = new Intent();
                                        intent.putExtra("DeptId",DeptId.getText().toString());
                                        intent.putExtra("AssetNumber",AssetNumber.getText().toString());
                                        intent.putExtra("preserver",preserver.getText().toString());
                                        intent.putExtra("place",place.getText().toString());
                                        Check_alter.this.setResult(6,intent);
                                        Check_alter.this.finish();//把item的内容返回给之前的activity
                                        Toast.makeText(Check_alter.this,"提交成功",Toast.LENGTH_SHORT).show();
                                    }
                                    if (json.getString("state").equals("0")){
                                        Toast.makeText(Check_alter.this,"提交失败",Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                    }
                });

            }
        }).start();
    }

    private void inview() {
        preserver_lt = (LinearLayout) findViewById(R.id.add_recipient);
        place_lt = (LinearLayout) findViewById(R.id.site);
        upload = (Button) findViewById(R.id.uploading);
        AssetNumber = (TextView) findViewById(R.id.AssetNumber);
        AssetName = (TextView) findViewById(R.id.AssetName);
        DeptId = (TextView) findViewById(R.id.DeptId);
        Type = (TextView) findViewById(R.id.Type);
        preserver = (TextView) findViewById(R.id.receive_recipient);
        place = (TextView) findViewById(R.id.site_tv);
        imageview = (ImageView) findViewById(R.id.fh);
        imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_title = (TextView) findViewById(R.id.title_text);
        tv_title.setText("修改");
        Intent intent = getIntent();
        AssetNumber.setText(intent.getStringExtra("AssetNumber"));
        AssetName.setText(intent.getStringExtra("AssetName"));
        DeptId.setText(intent.getStringExtra("DeptId"));
        Type.setText(intent.getStringExtra("Type"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==3){
            place.setText(data.getExtras().getString("site_name"));
            place_id = data.getExtras().getString("site_id");
            Log.i("333333333333",place_id);
        }
        if (requestCode==2&&resultCode==3){
            preserver.setText(data.getExtras().getString("recipient_name"));
            preserver_id = data.getExtras().getString("recipient_id");
            DeptId.setText(data.getExtras().getString("de_name"));
            de_id=data.getExtras().getString("de_id");
        }
    }
}
